package com.mep.rbac.security;

import com.mep.common.utils.FastJsonUtils;
import com.mep.rbac.dao.SysUserRoleMapper;
import com.mep.rbac.model.SysRole;
import com.mep.rbac.model.SysUser;
import com.mep.rbac.model.SysUserRole;
import com.mep.rbac.pojo.User;
import com.mep.rbac.service.SysRoleService;
import com.mep.rbac.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService implements UserDetailsService {

    //private List<User> userList;

//    private List<SysUser> sysUserList;

    @Autowired
    private SysUserService sysUserService;


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleService sysRoleService;

    //    @PostConstruct
//    public void initData(){
//        String password = passwordEncoder.encode("123456");
//        SysUser sysUser = sysUserService.findByName("admin");
//        System.out.println("sysuser:---------------------------->" + sysUser.toString());
//        redisTemplate.opsForValue().set("admin",sysUser);
//        Set<String> permissions = sysUserService.findPermissions("admin");
//        redisTemplate.opsForValue().set("admin-role",permissions);
//        userList = new ArrayList<>();
//        userList.add(new User("admin", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin")));
//        userList.add(new User("andy",password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
//        userList.add(new User("mark",password, AuthorityUtils.commaSeparatedStringToAuthorityList("client")));
////        sysUserList = new ArrayList<>();
//    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.findByName(username);

        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.findUserRoles(sysUser.getId());
        StringBuffer stringBuffer = new StringBuffer();
        for (SysUserRole sysUserRole : sysUserRoleList) {
            SysRole sysRole = sysRoleService.findById(sysUserRole.getRoleId());
            stringBuffer.append(sysRole.getName()).append(",");
        }
        String sysUsserFastJson = FastJsonUtils.convertObjectToJSON(sysUser);
        return new User(sysUsserFastJson, sysUser.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(stringBuffer.toString()));

    }
}
